function validateNomeCognome(val, name) {
	if(val) {
		for(var i=0; i<val.length; i++) {
			var char=val.charAt(i);
			if(char<'A' || ('Z'<char && char<'a') || char>'z') {
					var mess=name+" non valido.\n"
					return mess;
			}
		}
	}
	return "";
}


function validateMail(val) {
	if(val){
		for(var i=0; i<val.length; i++) {
			var char=val.charAt(i);
			if(char=='@') {
				return "";
			}
		}
		var mess="e-mail non valida.\n";
		return mess;
	}
}



function confermaMailPsw(val1, val2, name) {
	if(val1 && val2){
		var mess="errore nella conferma "+name+"\n";
		if(val1.length != val2.length){
			return mess;	
		} else {
			for(var i=0;i<val1.length; i++){
				if(val1.charAt(i)!=val2.charAt(i)){
					return mess;
				}
			}
		return "";
		}
		
	}
}


function controllaVuoti(){
	var mess="";
	if(document.modulo.nome.value.length==0){
		mess+="Inserisci NOME\n";
	}
	if(document.modulo.cognome.value.length==0){
		mess+="Inserisci COGNOME\n";
	}
	if(document.modulo.email.value==0){
		mess+="Inserisci E-MAIL\n";
	}
	if(document.modulo.conferma_email.value==0) {
		mess+="Inserisci CONFERMA_E-MAIL\n";
	}
	if(document.modulo.password.value==0){
		mess+="Inserisci PASSWORD\n";
	}
	if(document.modulo.conferma_password.value==0){
		mess+="Inserisci CONFERMA_PASSWORD\n";
	}
	if(document.modulo.corso.value==0){
		mess+="Inserisci CORSO\n";
	}
	return mess;
}


function passwordLength(pass,minNumber){
	if(pass){
		if(pass.length<minNumber){
			return "lunghezza minima password "+minNumber+" caratteri\n";
		}
		return "";
	}
}

function controllaForm(){

	 var cont=controlloFinale('show-message');
	 if(cont!=0){
		return false;
	 }
	
	var mess2="";
	mess2+=validateNomeCognome(document.modulo.nome.value,'nome');
	mess2+=validateNomeCognome(document.modulo.cognome.value,'cognome');
	mess2+=validateMail(document.modulo.email.value);
	mess2+=passwordLength(document.modulo.password.value,6);
	if(mess2!=""){
		alert(mess2);
		return false;
	}
	
	var mess3="";
	mess3=confermaMailPsw(document.modulo.email.value,document.modulo.conferma_email.value,'email');
	mess3+=confermaMailPsw(document.modulo.password.value,document.modulo.conferma_password.value,'password');
	if(mess3!=""){
		alert(mess3);
		return false;
	}
	return true;
	
}



function controlloPswPreliminare(id, minNumber, pass) {
	if(pass){
		if(pass.length<minNumber){
			document.getElementById(id).style.display="block";
		} else {
			document.getElementById(id).style.display="none";
		}
	}
	return false;
}

function controlloFinale(id) {
	var cont=0;
	var stars=new Array(7);
	stars[0]=document.getElementById('star-nome');
	stars[1]=document.getElementById('star-cognome');
	stars[2]=document.getElementById('star-email');
	stars[3]=document.getElementById('star-conferma-email');
	stars[4]=document.getElementById('star-password');
	stars[5]=document.getElementById('star-conferma-password');
	stars[6]=document.getElementById('star-corso');
	
	var fields=new Array(7);
	fields[0]=document.modulo.nome.value; 
	fields[1]=document.modulo.cognome.value; 
	fields[2]=document.modulo.email.value; 
	fields[3]=document.modulo.conferma_email.value; 
	fields[4]=document.modulo.password.value; 
	fields[5]=document.modulo.conferma_password.value; 
	fields[6]=document.modulo.corso.value; 
	
	for(var i=0; i<7;i++) {
		if(fields[i].length==0) {
			stars[i].style.color="#C00";
			cont++;
		} else {
			stars[i].style.color="#000";
		}
	}
	if(cont!=0){
		document.getElementById(id).style.display="block";
	}
	return cont;
}

	